/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.railway.reports;

import com.railway.reports.utils.CurrencyType;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.sf.dynamicreports.examples.Templates;
import net.sf.dynamicreports.jasper.builder.JasperReportBuilder;
import static net.sf.dynamicreports.report.builder.DynamicReports.cmp;
import static net.sf.dynamicreports.report.builder.DynamicReports.col;
import static net.sf.dynamicreports.report.builder.DynamicReports.exp;
import static net.sf.dynamicreports.report.builder.DynamicReports.grp;
import static net.sf.dynamicreports.report.builder.DynamicReports.sbt;
import static net.sf.dynamicreports.report.builder.DynamicReports.stl;
import static net.sf.dynamicreports.report.builder.DynamicReports.type;
import net.sf.dynamicreports.report.builder.column.TextColumnBuilder;
import net.sf.dynamicreports.report.builder.group.ColumnGroupBuilder;
import net.sf.dynamicreports.report.builder.style.StyleBuilder;
import net.sf.dynamicreports.report.constant.HorizontalAlignment;
import net.sf.dynamicreports.report.constant.VerticalAlignment;
import net.sf.dynamicreports.report.datasource.DRDataSource;
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.swing.JRViewer;

/**
 *
 * @author Moofie
 */
public class SalesReport {
    private ArrayList<Map<String, Object>> reportData;
    private JRViewer jvr;
    private JRViewer reportView;
    
    public void buildData(ArrayList<Map<String, Object>> reportData) {
        this.reportData = reportData;
    }
    
    public JRViewer buildReport() {
        try {
            CurrencyType currencyType = new CurrencyType();
            JRDataSource datasource = buildDataSource(reportData);
            StyleBuilder titleStyle = stl.style(Templates.boldCenteredStyle)
	                             .setVerticalAlignment(VerticalAlignment.MIDDLE)
	                             .setFontSize(13);
            JasperReportBuilder report = new JasperReportBuilder();
            TextColumnBuilder monthCol = col.column("Month", "month", type.dateYearToMonthType());
            TextColumnBuilder salesIDCol = col.column("Sales ID", "slogid", type.stringType());
            TextColumnBuilder salesDateCol = col.column("Sales Date", "salesDate", type.dateType());
            TextColumnBuilder staffIDCol = col.column("User ID", "madeBy", type.stringType());
            TextColumnBuilder transactionTypeCol = col.column("Transaction Type", "transactionType", type.stringType());
            TextColumnBuilder salesAmountCol = col.column("Sales Amount", "salesAmount", currencyType);
//            TextColumnBuilder ssalesAmountCol = col.column("Saless Amount", "salesAmount", type.doubleType());
            report.setTemplate(Templates.reportTemplate).columns(
                    monthCol,
                    salesIDCol.setHorizontalAlignment(HorizontalAlignment.CENTER),
                    salesDateCol.setHorizontalAlignment(HorizontalAlignment.CENTER),
                    staffIDCol.setHorizontalAlignment(HorizontalAlignment.CENTER),
                    transactionTypeCol.setHorizontalAlignment(HorizontalAlignment.CENTER),
                    salesAmountCol.setHorizontalAlignment(HorizontalAlignment.CENTER))
                    .title(cmp.horizontalList().add(
                    cmp.image(this.getClass().getResourceAsStream("/com/railway/image/logo.png")).setFixedDimension(192, 70),
                    cmp.text("Sales Report").setStyle(titleStyle).setHorizontalAlignment(HorizontalAlignment.LEFT),
                    cmp.text(new Date()).setStyle(titleStyle).setHorizontalAlignment(HorizontalAlignment.RIGHT)).newRow()
                    .add(cmp.filler().setStyle(stl.style().setTopBorder(stl.pen2Point())).setFixedHeight(10)))
                    .pageFooter(cmp.pageXofY());
            ColumnGroupBuilder itemGroup = grp.group(monthCol);
            itemGroup.setPrintSubtotalsWhenExpression(
                    exp.printWhenGroupHasMoreThanOneRow(itemGroup));
            report.subtotalsAtSummary(sbt.sum(salesAmountCol)).subtotalsAtFirstGroupFooter(sbt.sum(salesAmountCol));
            report.groupBy(itemGroup);
            report.setDataSource(datasource);
            JasperPrint jsp = report.toJasperPrint();
            reportView = new JRViewer(jsp);
            reportView.setZoomRatio(0.8f);
            return reportView;
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }
    
 
    private JRDataSource buildDataSource(ArrayList<Map<String, Object>> data) {
        DRDataSource ds;
        SimpleDateFormat format = new SimpleDateFormat("MMMMM yyyy");
        ds = new DRDataSource("month", "slogid", "salesDate", "madeBy", "transactionType", "salesAmount");
        for (int i = 0; i < data.size(); i++) {
            try {
                Map<String, Object> map = data.get(i);
                ds.add(
                        format.parse(format.format(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(map.get("salesDate").toString()))),
                        map.get("slogid"),
                        new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(map.get("salesDate").toString()),
                        map.get("madeBy"),
                        map.get("transactionType"),
                        new BigDecimal(map.get("salesAmount").toString()));
            } catch (ParseException ex) {
                Logger.getLogger(TicketReport.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return ds;
    }
    
    public JRViewer getReport() {
        return this.jvr;
    }
}
